package com.outsourcingmanagesystem.controller;

import com.outsourcingmanagesystem.common.JwtClaimsConstant;
import com.outsourcingmanagesystem.common.JwtUtil;
import com.outsourcingmanagesystem.common.Result;
import com.outsourcingmanagesystem.entity.dto.UserLoginDTO;
import com.outsourcingmanagesystem.entity.User;
import com.outsourcingmanagesystem.entity.vo.UserLoginVO;
import com.outsourcingmanagesystem.property.JwtProperties;
import com.outsourcingmanagesystem.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tags(@Tag(name = "用户模块"))
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    //密码为123456

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("开始登录");
         User user =  userService.login(userLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, user.getUserId());

        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(Long.valueOf(user.getUserId()))
                .username(user.getUsername())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
    //修改用户表status字段
    @Operation(summary = "申请上线")
    @PostMapping()
    public Result<UserLoginVO> updateStatus(@RequestBody User user){
        log.info("开始修改用户表status字段");
        userService.updateStatus(user);
        return Result.success();
    }

    //显示外包人员个人资料
    @Operation(summary = "个人资料展示")
    @GetMapping("/show")
    public Result<User> show(User user){
        log.info("开始显示外包人员个人资料");
        userService.show(user);
        return Result.success();
    }


    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<UserLoginVO> register(@RequestBody UserLoginDTO userLoginDTO){
        log.info("开始注册");
        userService.register(userLoginDTO);
        return Result.success();
    }
    @Operation(summary = "用户登出")
    @PostMapping("/logout")  // "token:xxx"
    public Result<UserLoginVO> logout(){
        return Result.success();
    }

    @Operation(summary = "申请下班")
    @PostMapping("/offWork")
    public Result<UserLoginVO> offWork(@RequestBody User user){
        log.info("开始申请下班");
        userService.offWork(user);
        return Result.success();
    }
}
